import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      component: () => import('@/views/login.vue'),
      meta:{
        title: '登陆'
      }
    },
    {
      path:'/register',
      component: () => import('@/views/register.vue'),
      meta:{
        title: '注册'
      }
    },
    {
      path:'/',
      redirect: 'noteClass'
    },
    {
      path:'/noteClass',
      component:()=>import('@/views/noteClass.vue'),
      meta:{
        title: '笔记分类'
      }
    },
    {
      path:'/noteList',
      component:()=>import('@/views/noteList.vue')
    },
    {
      path:'/noteDetail',
      component:()=>import('@/views/noteDetail.vue'),
      meta:{
        title: '笔记详情'
      }
    },
    {
      path:'/notePublish',
      component:()=>import('@/views/notePublish.vue'),
      meta:{
        title: '笔记发布'
      }
    }
  ]
})

// 全局的路由守卫
const whitePath = ['/login','/register','/noteClass']
router.beforeEach((to,from,next)=>{
  // console.log(to);
  if(!whitePath.includes(to.path)){ //需要登陆
    // 判断浏览器本地有无userInfo
    if(!localStorage.getItem('userInfo')){ // 没登陆
      router.push('/login')
      return
    }
    next()
    return
  }
  next()
})

export default router
